const Koa = require('koa'),
  less = require('koa-less2x'),
  serve = require('koa-static'),
  path = require('path'),
  koaBody = require('koa-body'),
  userRouter = require('./src/routes/user'),
  adminRouter = require('./src/routes/admin'),
  views = require('koa-views'),
  mongo = require('koa-mongo'),
  prodConfig = require('./src/config/config.json'),
  devConfig = require("./src/config/devConfig.json");

console.log(process.env.NODE_ENV);

const config = process.env.NODE_ENV === "dev" ? devConfig : prodConfig;

const app = new Koa();

app.keys = ['Blog'];

app.use(koaBody({
  multipart: true,
  formidable: {
    maxFileSize: 10 * 1024 * 1024	// 设置上传文件大小最大限制，默认2M
  }
}));

// template engine
app.use(views(__dirname + '/src/views', {
  map: {
    njk: 'nunjucks'
  },
  options: {
    nunjucks: {
      loader: path.join(__dirname, 'src/views')
    }
  }
}));

// mongodb
app.use(mongo(config.dbConfig));

// user and admin routes
app.use(userRouter.routes());
app.use(adminRouter.routes());

// user adn admin *.css
app.use(less('./src/public'));

// static files
app.use(serve(__dirname + '/src/public'));

app.listen(8081, () => {
  console.log(`Blog application is listening http://localhost:8081`);
});
